package com.dvt.faceScan;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import com.dvt.faceScan.httpUntil.PostThread;
import com.dvt.faceScan.widget.Config;

import java.net.URLEncoder;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

/**
 * Created by XGJ on 2018/3/20.
 * 人脸识别
 */

public class FaceDiscern extends AppCompatActivity{
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    public static String identify(HashMap<String, String> bmp) {
        Iterator it = bmp.entrySet().iterator();
        Map.Entry entry = (Map.Entry) it.next();
        String bmpImg = (String)entry.getValue();  //强转

        // 请求url
        String url = "https://aip.baidubce.com/rest/2.0/face/v2/identify";
        try {
            String imgParam = URLEncoder.encode(bmpImg, "UTF-8");
            String param = "group_id=" + "test_group_2" + "&user_top_num=" + "1" + "&face_top_num=" + "1" + "&images=" + imgParam ;
            // 注意这里仅为了简化编码每一次请求都去获取access_token，线上环境access_token有过期时间， 客户端可自行缓存，过期后重新获取。
            String accessToken = Config.token;
            //起一个子线程
            PostThread pt = new PostThread();
            pt.setAccessToken(accessToken);
            pt.setParam(param);
            pt.setUrl(url);
            pt.start();
            String result = null;
            try {
                while(true){
                    if(pt.getResult()==null){
                        Thread.currentThread().sleep(1000);
                    }else{
                        result = pt.getResult();
                        break;
                    }
                }
            }catch (Exception e){
                e.printStackTrace();
            }
            return result;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }




}
